//
//  DetailsViewController.m
//  A段项目
//
//  Created by 官琛 on 15-6-20.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailsViewController.h"

@interface DetailsViewController ()

@end

@implementation DetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
//    self.tableView.frame = CGRectMake(0, 40, WIDTH, HEIGHT);
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    

    //取消换航线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    
    
    self.title1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    self.title1.textAlignment = NSTextAlignmentCenter;
    self.title1.font = [UIFont boldSystemFontOfSize:20];
    
    [view1 addSubview:self.title1];
    [_title1 release];
    self.tableView.tableHeaderView = view1;
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    
    self.arr = [NSMutableArray array];
    self.array = [NSMutableArray array];
    
    self.number = 1;
    [self afnGetRequest];
    [self footerRefesh];
    
   
    [view1 release];
  
    [_tableView autorelease];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-unie642.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
}
- (void)leftBarButtonItemAction
{
    [self.navigationController popViewControllerAnimated:YES];
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellstr = @"DetailsCell";
    DetailsTableViewCell *Dcell = [tableView dequeueReusableCellWithIdentifier:cellstr];
    if (Dcell == nil) {
        Dcell = [[DetailsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellstr];
    }
   
    Cemera *cemer = [self.arr objectAtIndex:indexPath.row];
    Dcell.cemCell = cemer;
    Dcell.myDelegate = self;
    Dcell.selectionStyle = UITableViewCellSelectionStyleNone;
    return Dcell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonViewController *view = [[PersonViewController alloc]init];
    view.personId = [[self.array objectAtIndex:indexPath.row]integerValue];
    [self.navigationController pushViewController:view  animated:YES];
    [view release];
//    NSLog(@"personID = %ld",self.perId);

}

-(void)bringPersonId
{
    //push 协议方法
    

}


//上拉加载
- (void)footerRefesh
{
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        //        NSLog(@"%ld", self.number);
        self.isUpLoading = YES;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest];
        
        [self.tableView reloadData];
        [self.HUD hide:YES];
        // 拿到当前的下拉刷新控件，结束刷新状态
    }];
    
    
}


-(void)afnGetRequest
{

    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.m.mtime.cn/TopList/TopListDetails.api?pageIndex=%ld&topListId=%ld", self.number, self.detailsID];
    
    self.number++;
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"persons"];
        
        for (NSMutableDictionary *dic in array) {
            Cemera *ceme = [[Cemera alloc]init];
            ceme.summary = [dic objectForKey:@"summary"];
            ceme.posterUrl = [dic objectForKey:@"posterUrl"];
            ceme.birthDay = [dic objectForKey:@"birthDay"];
            ceme.birthLocation = [dic objectForKey:@"birthLocation"];
            ceme.nameCn = [dic objectForKey:@"nameCn"];
            ceme.nameEn = [dic objectForKey:@"nameEn"];
            ceme.sex = [dic objectForKey:@"sex"];
            ceme.idd = [[dic objectForKey:@"id"]integerValue];
            ceme.type = [[dic objectForKey:@"type"]integerValue];
            ceme.rankNum = [[dic objectForKey:@"rankNum"]integerValue];
            ceme.rating = [[dic objectForKey:@"rating"]floatValue];
            [self.arr addObject:ceme];
            
            [self.array addObject:[NSString stringWithFormat:@"%ld",ceme.idd]];
            
            self.title1.text = self.cem.topListNameCn;
            [ceme release];
        }
        [self.tableView reloadData];
        
        [self.HUD hide:YES];
        [self.tableView.footer endRefreshing];
        
        NSLog(@"%ld", self.number);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
